package net.oschina.bilbodai.common.beanutil.bean.codedump;

import net.oschina.bilbodai.common.beanutil.bean.BeanDumpUtil;
import net.oschina.bilbodai.common.beanutil.bean.DumpBeanSample;
import net.oschina.bilbodai.common.beanutil.bean.IBeanAccessor;

import java.util.LinkedList;
import java.util.List;

/**
 * @author daimao  Date: 16/5/27 Time: 下午1:16
 * @version $Id$
 */
public class CollectionIterCopy {

    public static class Foo {
        List<DumpBeanSample.Foo> foos;

        public List<DumpBeanSample.Foo> getFoos() {
            return foos;
        }

        public void setFoos(List<DumpBeanSample.Foo> foos) {
            this.foos = foos;
        }
    }

    public static class FooTarget {
        List<DumpBeanSample.Foo> fooTargets;

        public List<DumpBeanSample.Foo> getFooTargets() {
            return fooTargets;
        }

        public void setFooTargets(List<DumpBeanSample.Foo> fooTargets) {
            this.fooTargets = fooTargets;
        }
    }

    public Object propertyDump(Object src, Object target) {

        Foo foo = (Foo) src;
        FooTarget fooTarget = (FooTarget) target;

        if (foo.getFoos() != null) {
            if (fooTarget.getFooTargets() == null) {
                fooTarget.setFooTargets(new LinkedList<DumpBeanSample.Foo>());
            }
            IBeanAccessor<DumpBeanSample.Foo, DumpBeanSample.Foo> accessor = BeanDumpUtil.create(DumpBeanSample.Foo.class, DumpBeanSample.Foo.class);
            List<DumpBeanSample.Foo> fooTargets = fooTarget.getFooTargets();
            for (DumpBeanSample.Foo foo1 : foo.getFoos()) {
                fooTargets.add(accessor.propertyDump(foo1, null));
            }
        }

        return fooTarget;
    }

}